package bigapps.myneighbor.dao.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import bigapps.myneighbor.dao.ParkingFacilityDao;
import bigapps.myneighbor.domain.ParkingFacility;

@Repository(value = "parkingFacilityDao")
public class ParkingFacilityDaoImpl extends GenericEntityManagerDao<ParkingFacility, Long> implements ParkingFacilityDao {
	@Override
	public List<ParkingFacility> findAll(){
		Query query =getEntityManager().createQuery("select pf from ParkingFacility pf where pf.status = 1");
		List<ParkingFacility> listOfParkingInfo = query.getResultList();

		return listOfParkingInfo;
	}
	public List<ParkingFacility> findAllStatus(){
		Query query =getEntityManager().createQuery("select pf from ParkingFacility pf");
		List<ParkingFacility> listOfParkingInfo = query.getResultList();
		for(ParkingFacility pf : listOfParkingInfo){
			System.out.println(pf.getEntityId());
		}

		return listOfParkingInfo;
	}
	public List<ParkingFacility> getUnassignedLatitudeLongitude()
	{
		Query query =getEntityManager().createQuery("select pf from ParkingFacility pf where pf.status = 0");
		List<ParkingFacility> listOfParkingInfo = query.getResultList();
		return listOfParkingInfo;

	}
	//	@Transactional
	public ParkingFacility findEntity(ParkingFacility pf)
	{
		try{
			Query query = getEntityManager().createQuery("select pf from ParkingFacility pf where pf.licenseNumber = :licenseNumber");
			query.setParameter("licenseNumber", pf.getLicenseNumber());
			return (ParkingFacility)query.getSingleResult();
		}
		catch(Exception e)
		{
			return null;
		}
	}

	public List<ParkingFacility> getErroredLatitudeLongitude()
	{
		Query query =getEntityManager().createQuery("select pf from ParkingFacility pf where pf.status > 1 and pf.status < 50");
		List<ParkingFacility> listOfParkingInfo = query.getResultList();
		return listOfParkingInfo;

	}

	public List<ParkingFacility> findByStatus(int status)
	{
		String queryStr = null;
		if(status > 1)
		{
			queryStr = "select pf from ParkingFacility pf where pf.status > 1";
		}
		else
		{
			queryStr = "select pf from ParkingFacility pf where pf.status = :status";
		}
		Query query = getEntityManager().createQuery(queryStr);
		query.setParameter("status", status);
		return query.getResultList();
	}
}
